package com.stockz.portfolio.manager;

import com.stockz.portfolio.domain.model.Holding;
import com.stockz.portfolio.domain.model.Portfolio;
import com.stockz.portfolio.domain.model.Transaction;

public class TransactionManager {

	
	public void execute(Transaction transaction, Portfolio portfolio){
		if(transaction != null && portfolio != null){
			Holding holding = portfolio.getHolding(transaction.getScrip());
			execute(transaction, holding);
			portfolio.getTransactions().add(transaction);
			portfolio.setDirty(true);
		}
	}
	
	private void execute(Transaction transaction, Holding holding){
		if(transaction != null && holding != null){
			switch(transaction.getType()){
			case BUY :
				holding.setQuantity(holding.getQuantity() + transaction.getQuantity());
			case COVER :
				holding.setQuantity(holding.getQuantity() - transaction.getQuantity());
			case SELL :
				holding.setQuantity(holding.getQuantity() - transaction.getQuantity());
			case SHORT : 
				holding.setQuantity(holding.getQuantity() + transaction.getQuantity());
			}
		}
	}
	
	
}
